package com.huiminpay.paymentagent.message;

import com.alibaba.fastjson.JSON;
import com.huiminpay.paymentagent.api.dto.PaymentResponseDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 生成消息类
 */
@Component
@Slf4j
public class PayProducer {
    // 订单结果查询主题
    private static final String TOPIC_ORDER = "TP_PAYMENT_ORDER";

    @Resource
    RocketMQTemplate rocketMQTemplate;

    //发送消息（查询支付宝、微信订单状态）
    public void payOrderNotice(PaymentResponseDTO paymentResponseDTO) {
        // 发送延迟消息
        Message<PaymentResponseDTO> responseMessage = MessageBuilder.withPayload(paymentResponseDTO).build();
        //延迟第3级发送(延迟10秒)
        SendResult sendResult = rocketMQTemplate.syncSend(TOPIC_ORDER, responseMessage, 1000, 3);
        log.info("支付渠道代理服务向MQ发送订单查询的消息：{}",JSON.toJSONString(paymentResponseDTO));
    }

    //订单结果 主题
    private static final String TOPIC_RESULT = "TP_PAYMENT_RESULT";
    //发送消息（最新的支付结果通知）
    public void payResultNotice(PaymentResponseDTO paymentResponseDTO) {
        rocketMQTemplate.convertAndSend(TOPIC_RESULT, paymentResponseDTO);
        log.info("支付渠道代理服务向MQ发送订单结果的消息：{}",JSON.toJSONString(paymentResponseDTO));
    }

}
